﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MimallAPI.Dal;
using MimallAPI.Model;
using System.IO;
using Alipay.AopSdk.AspnetCore;
using Alipay.AopSdk.Core.Domain;
using Alipay.AopSdk.Core.Request;
using Alipay.AopSdk.Core.Response;

namespace MimallAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        //接口注入
        private readonly IGoods _goods;
        private readonly AlipayService _alipayService;
        public GoodsController(IGoods goods, AlipayService alipayService)
        {
            _goods = goods;
            _alipayService = alipayService;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost,Route("login")]
        public IActionResult Login(UserInfo userInfo)
        {
            return Ok(_goods.Login(userInfo));
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult registered(UserInfo u)
        {
            return Ok(_goods.registered(u));
        }
        /// <summary>
        /// 商品列表分页显示
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetGood(int pageindex, int pagesize)
        {
            var totalcount = 0;
            var list = _goods.GetGood(out totalcount, pageindex, pagesize);
            return Ok(new { totalcount, list });
        }
        /// <summary>
        /// 商品列表展示
        /// </summary>
        /// <param name="GoodId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult EditGood(int GoodId)
        {
            return Ok(_goods.EditGood(GoodId));
        }
        /// <summary>
        /// 添加到购物车
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [HttpPost]
       public IActionResult AddShopCar(ShopCar s)
        {
            return Ok(_goods.AddShopCar(s));
        }
        /// <summary>
        /// 购物车显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShopCar(int UserId)
        {
            return Ok(_goods.GetShopCar(UserId));
        }

        /// <summary>
        /// 逻辑删除购物车数据
        /// </summary>
        /// <param name="CarId"></param>
        /// <returns></returns>
        [HttpPost]
      public IActionResult DelShopcar(int CarId)
        {
            return Ok(_goods.DelShopcar(CarId));
        }

        /// <summary>
        /// 更改数量
        /// </summary>
        /// <param name="CarId"></param>
        /// <param name="CarCount"></param>
        /// <returns></returns>
        [HttpPost]
       public IActionResult editcount(int CarId, int CarCount)
        {
            return Ok(_goods.editcount(CarId, CarCount));
        }
        /// <summary>
        /// 地址联动
        /// </summary>
        /// <param name="Fid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Getsite(int Fid)
        {
            return Ok(_goods.Getsite(Fid));
        }
        /// <summary>
        /// 收货信息显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRece()
        {
            return Ok(_goods.GetRece());
        }
        /// <summary>
        /// 添加收货信息
        /// </summary>
        /// <param name="r"></param>
        /// <returns></returns>
       [HttpPost]
        public IActionResult addRece(ReceInfo r)
        {
            return Ok(_goods.addRece(r));
        }
        /// <summary>
        /// 删除收货信息
        /// </summary>
        /// <param name="ReceId"></param>
        /// <returns></returns>
        [HttpDelete]
       public IActionResult DelRece(int ReceId)
        {
            return Ok(_goods.DelRece(ReceId));
        }

        /// <summary>
        /// 订单列表添加
        /// </summary>
        /// <param name="adi"></param>
        /// <returns></returns>
      [HttpPost]
        public IActionResult AddInde(AddIndent adi)
        {
            return Ok(_goods.AddInde(adi));
        }
        [HttpGet]
       public IActionResult GetIndent()
        {
            return Ok(_goods.GetIndent());
        }
        [HttpGet]
        public IActionResult DDTJ()
        {
            return Ok(_goods.DDTJ());
        }

        [HttpGet]
        public IActionResult AliPayShow()
        {
            // 外部订单号，商户网站订单系统中唯一的订单号
            string out_trade_no = System.Guid.NewGuid().ToString();

            // 订单名称
            string subject = "测试商品";

            // 付款金额
            string total_amount = "0.1";

            // 商品描述
            string body = "无描述";

            // 组装业务参数model
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.Body = body;
            model.Subject = subject;
            model.TotalAmount = total_amount;
            model.OutTradeNo = out_trade_no;
            model.ProductCode = "FAST_INSTANT_TRADE_PAY";

            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            // 设置同步回调地址
            request.SetReturnUrl("");
            // 设置异步通知接收地址
            request.SetNotifyUrl("");
            // 将业务model载入到request
            request.SetBizModel(model);

            AlipayTradePagePayResponse response = null;
            try
            {

                response = _alipayService.PageExecute(request, null, "post");
                //Response.Write(response.Body);
            }
            catch (Exception exp)
            {
                throw exp;
            }
            return Ok(response.Body);
        }
    }
}
